package com.tanhua.dubbo.api;

import cn.hutool.core.collection.CollUtil;
import com.mongodb.client.result.DeleteResult;
import com.tanhua.model.mongo.UserLike;
import com.tanhua.model.vo.PageResult;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.util.StringUtils;

import java.util.Collection;
import java.util.Collections;
import java.util.List;

@DubboService
public class UserLikeApiImpl implements UserLikeApi {
    @Autowired
    private MongoTemplate mongoTemplate;

    //删除喜欢
    @Override
    public void delectLike(Long likeuid, Long userId) {
        Query query = Query.query(Criteria.where("userId").is(userId).and("likeUserId").is(likeuid).and("isLike").is(true));
        mongoTemplate.remove(query, UserLike.class);
    }

    //喜欢我的粉丝
    @Override
    public List<UserLike> findlikeme(Integer type, Integer page, Integer pagesize, Long userId) {
        Query query = Query.query(Criteria.where("likeuserId").is(userId).and("isLike").is(true)).skip((page - 1) * pagesize).limit(pagesize);
        List<UserLike> userLikes = mongoTemplate.find(query, UserLike.class);
        return userLikes;
    }

    //查询互相喜欢列表
    @Override
    public List findLiketolike(Integer type, Integer page, Integer pagesize, String nickname, Long userId) {

        Query query1 = Query.query(Criteria.where("userId").is(userId).and("isLike").is(true));
        List<UserLike> userLikes1 = mongoTemplate.find(query1, UserLike.class);
        List<Long> likeUserId = CollUtil.getFieldValues(userLikes1, "likeUserId", Long.class);
        Query query2 = Query.query(Criteria.where("userId").in(likeUserId).and("isLike").is(true)).skip((page - 1) * pagesize).limit(pagesize);
        List<UserLike> userLikes2 = mongoTemplate.find(query2, UserLike.class);
        return userLikes2;
    }

    //查询我喜欢的用户信息
    @Override
    public List<UserLike> findMylike(Integer type, Integer page, Integer pagesize, Long userId) {
        Query query = Query.query(Criteria.where("userId").is(userId).and("isLike").is(true)).skip((page - 1) * pagesize).limit(pagesize);
        List<UserLike> userLikes = mongoTemplate.find(query, UserLike.class);
        return userLikes;
    }


    @Override
    public Boolean saveOrUpdate(Long userId, Long likeUserId, boolean isLike) {
        try {
            //1、查询数据
            Query query = Query.query(Criteria.where("userId").is(userId).and("likeUserId").is(likeUserId));
            UserLike userLike = mongoTemplate.findOne(query, UserLike.class);
            //2、如果不存在，保存
            if (userLike == null) {
                userLike = new UserLike();
                userLike.setUserId(userId);
                userLike.setLikeUserId(likeUserId);
                userLike.setCreated(System.currentTimeMillis());
                userLike.setUpdated(System.currentTimeMillis());
                userLike.setIsLike(isLike);
                mongoTemplate.save(userLike);
            } else {
                //3、更新
                Update update = Update.update("isLike", isLike)
                        .set("updated", System.currentTimeMillis());
                mongoTemplate.updateFirst(query, update, UserLike.class);
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public Integer myLike(Long userId) {
        Query query = Query.query(Criteria.where("userId").is(userId).and("isLike").is(true));
        Long count = mongoTemplate.count(query, UserLike.class);
        return Integer.parseInt(count.toString());
    }

    @Override
    public Integer likeMe(Long userId) {
        Query query = Query.query(Criteria.where("likeuserId").is(userId).and("isLike").is(true));
        Long count = mongoTemplate.count(query, UserLike.class);
        return Integer.parseInt(count.toString());
    }

    @Override
    public Integer likeTOlike(Long userId) {
        Query query1 = Query.query(Criteria.where("userId").is(userId).and("isLike").is(true));
        List<UserLike> userLikes1 = mongoTemplate.find(query1, UserLike.class);
        List<Long> likeUserId = CollUtil.getFieldValues(userLikes1, "likeUserId", Long.class);
        Query query2 = Query.query(Criteria.where("userId").in(likeUserId).and("isLike").is(true));
        List<UserLike> userLikes2 = mongoTemplate.find(query2, UserLike.class);
        Long count = userLikes2.stream().filter(c -> c.getLikeUserId() == userId).count();
        return count.intValue();
    }


}
